package client.GUI.dashboard;


import javax.swing.JPanel;
import java.awt.GridLayout;

/**
 * Generates the dashboard panel - the main screen
 */
public class DashboardPanel extends JPanel {
	
	private AccountsPanel accountsPanel;
	private PricesPanel pricesPanel;
	private OrderHistoryPanel orderHistoryPanel;
	private OpenOrderPanel openOrderPanel;
	private static DashboardPanel instance = null;
	
	/**
	 * Default constructor, sizes the dashboard and
	 * sets the layout
	 */
	private DashboardPanel() {
		super();
		this.setSize(1024, 700);
		this.setLayout(new GridLayout(2,2));
	}
	
	/**
	 * Gets an instance for DashboardPanel
	 */
	public static DashboardPanel getInstance() {
		 if (instance == null) {
				instance = new DashboardPanel();
			}
			return instance;
	}
	
	/**
	 * Initializes the dashboard
	 */
	public void initialize() {
		pricesPanel = PricesPanel.getInstance();
		accountsPanel = AccountsPanel.getInstance();
		orderHistoryPanel = OrderHistoryPanel.getInstance();
		openOrderPanel = OpenOrderPanel.getInstance();
		
		//add the 4 different panels that compose the dashboard
		this.add(openOrderPanel);
		this.add(pricesPanel);
		this.add(orderHistoryPanel);
		this.add(accountsPanel);
	}
	
	/**
	 * Return the accounts panel.
	 * 
	 * @return 	accountsPanel - the JPanel that holds the accounts table
	 * @see		AccountsPanel
	 */
	public AccountsPanel getAccountsPanel() {
		return accountsPanel;
	}
	
	/**
	 * Returns the prices panel.
	 * 
	 * @return 	pricesPanel - the currency ratios JPanel
	 * @see		PricesPanel
	 */
	public PricesPanel getPricesPanel() {
		return pricesPanel;
	}
	
	/**
	 * Returns the order history panel.
	 * 
	 * @return 	orderHistoryPanel - the order history table JPanel
	 * @see		OrderHistoryPanel
	 */
	public OrderHistoryPanel getOrderHistoryPanel() {
		return orderHistoryPanel;
	}
	
	/**
	 * Returns the open order panel
	 * 
	 * @return 	openOrderPanel		the open order table JPanel
	 * @see 	OpenOrderPanel
	 */
	public OpenOrderPanel getOpenOrderPanel() {
		return openOrderPanel;
	}
	
	/**
	 * Sets the visibility of the dashboard and 
	 * regenerate all the tables.
	 * 
	 * @param 	arg0	The boolean that will tell the method
	 * 					whether or not to refresh the tables
	 * 					and set the panel as visible	
	 */
	public void setVisible(boolean arg0) {
		super.setVisible(arg0);
		if (arg0 == true) {
			refreshTables();
			pricesPanel.updatePrices();
			pricesPanel.setStop(false);
			new Thread(pricesPanel).start();
		}
	}
	
	/**
	 * Refreshes the tables displayed in the dashboard
	 */
	public void refreshTables() {
		openOrderPanel.refreshTable();
		orderHistoryPanel.refreshTable();
		//accountsPanel.refreshTable();
	}
}
